import { OnInit, Injector, Component, Output, ViewChild, ElementRef, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { StockZT_RoomServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import { TreeNode, MenuItem } from 'primeng/api';
import { ArrayToTreeConverterService } from '@shared/utils/array-to-tree-converter.service';
import { TreeDataHelperService } from '@shared/utils/tree-data-helper.service';
import * as _ from 'lodash';
//import { LocationComponent } from './location.component';
import { IBasicRoomInfo } from './basic-room-info';
import { UserDataPermissionServiceProxy } from '@shared/service-proxies/stock-service-proxies';

export interface IBasicRoomOnTree extends IBasicRoomInfo {
    id: number;
    // parent: string | number;
    // code: string;
    displayName: string;
    // memberCount: number;
    // roleCount: number;
    // text: string;
    // state: any;
}
@Component({
    selector: 'room-tree',
    styleUrls: ['./room-tree.component.less'],
    templateUrl: './room-tree.component.html'
})
export class RoomTreeComponent extends AppComponentBase implements OnInit {
    @Output() ouSelected = new EventEmitter<IBasicRoomInfo>();
    //@ViewChild('location') location: LocationComponent;
    //@ViewChild('createOrEditDataItemModal') createOrEditDataItemModal: CreateOrEditDataItemModalComponent;
    constructor(
        injector: Injector,
        private _roomService: StockZT_RoomServiceProxy,
        private _arrayToTreeConverterService: ArrayToTreeConverterService,
        private _treeDataHelperService: TreeDataHelperService,
        private _userDataPermissionServiceProxy: UserDataPermissionServiceProxy
    ) {
        super(injector);
    }

    treeData: any;
    selectedItem: TreeNode;
    selecttept;
    itemContextMenuItems: MenuItem[];

    ngOnInit(): void {
        this._userDataPermissionServiceProxy.getUserDataPermissions('Warehouses').subscribe(result => {
            this.selecttept = result.items;
            // this.itemContextMenuItems = this.getContextMenuItems();
            this.getTreeDataFromServer();
        });
    }

    nodeSelect(event) {
        this.ouSelected.emit(<IBasicRoomInfo>{
            id: event.node.data.value,
            displayName: event.node.data.displayName
        });
    }

    nodeDrop(event) {}

    reload(event): void {
        this.getTreeDataFromServer();
    }

    public getTreeDataFromServer(): void {
        this._roomService.getRoomListTree(this.selecttept).subscribe(result => {
            this.treeData = this._arrayToTreeConverterService.createTree(
                result.items,
                'parentId',
                'id',
                null,
                'children',
                [
                    {
                        target: 'label',
                        targetFunction(item) {
                            return item.displayName;
                        }
                    },
                    {
                        target: 'expandedIcon',
                        value: 'fa fa-folder-open m--font-warning'
                    },
                    {
                        target: 'collapsedIcon',
                        value: 'fa fa-folder m--font-warning'
                    },
                    {
                        target: 'selectable',
                        value: true
                    }
                ]
            );
        });
    }

    deleteUnit(id) {
        let node = this._treeDataHelperService.findNode(this.treeData, { data: { id: id } });
        if (!node) {
            return;
        }

        if (!node.data.parentId) {
            _.remove(this.treeData, {
                data: {
                    id: id
                }
            });
        }

        let parentNode = this._treeDataHelperService.findNode(this.treeData, { data: { id: node.data.parentId } });
        if (!parentNode) {
            return;
        }

        _.remove(parentNode.children, {
            data: {
                id: id
            }
        });
    }
}
